import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class make reference to the variety of enemies on the game.
 * 
 * @author (Arturo Aaron Rios Obregon) 
 * @version (29 june 2013)
 */
public class Enemy extends Actor
{
    private int vida;
    enum Direccion{L,R,U,D}; //L (left) R (right) U (up) D (down)
    Direccion dir;
    private static final int move = 60;
    private static final int maxY = 250;
    private int moveE;
    private SimpleTimer timer;

    /**
     * Constructor
     */
    public Enemy(int life)
    {
        vida = life;
        dir=Direccion.D;
        timer = new SimpleTimer();
        timer.mark();
    }

    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }
   
    /**
     * This method create a random item when a enemy life is zero.
     */
    public void creaItem()
    {
        if(vida==0)
        {
            escogeItem();
        }
    }
    
    /**
     * This method create and choose a random item.
     */
    public void escogeItem()
    {
        int num= Greenfoot.getRandomNumber(42);
        if(num==0||num==14||num==22||num==30)
        {
            L it = new L();
            getWorld().addObject(it,getX(),getY());
        }
        if(num==3||num==12||num==26||num==34)
        {
            H it = new H();
            getWorld().addObject(it,getX(),getY());
        }
        if(num==6||num==9||num==23||num==35)
        {
            R it = new R();
            getWorld().addObject(it,getX(),getY());
        }
        if(num==1||num==11||num==25||num==38)
        {
            Gas it = new Gas();
            getWorld().addObject(it,getX(),getY());
        }
        if(num==7||num==40)
        {
            Ammo it = new Ammo();
            getWorld().addObject(it,getX(),getY());
        }
        if(num==5)
        {
            Bomb it = new Bomb();
            getWorld().addObject(it,getX(),getY());
        }
        if(num==15||num==32)
        {
            Shield it = new Shield();
            getWorld().addObject(it,getX(),getY());
        }
    }
    
    /**
     * This method check if a enemy life is zero and if did it, will be eliminated and up the score.
     */
    public void vidaCero()
    {
        if(vida==0)
        {
           
            if(this instanceof NormalEnemy)
                ((Juego)getWorld()).aumentaPuntos(200);
            else if(this instanceof MotherShip)
                ((Juego)getWorld()).aumentaPuntos(5000);
            else if(this instanceof ProducerEnemy)
                ((Juego)getWorld()).aumentaPuntos(500);
            else if(this instanceof Meteor)
                ((Juego)getWorld()).aumentaPuntos(50);
            else if(this instanceof BasicEnemy)
                ((Juego)getWorld()).aumentaPuntos(100);
            else if(this instanceof DupEnemy)
                ((Juego)getWorld()).aumentaPuntos(250);
            getWorld().removeObject(this);    
        }
    }
    
    /**
     * This method check if the enemy touch a bullet.
     */
    public void tocaBala()
    {
        if(getOneIntersectingObject(Bullet.class)!=null)
        {
            vida=vida-((Bala)getOneIntersectingObject(Bullet.class)).getDamage();
            getWorld().removeObject(getOneIntersectingObject(Bullet.class));
        }
        if(getOneIntersectingObject(SimpleBullet.class)!=null)
        {
            vida=vida-((Bala)getOneIntersectingObject(SimpleBullet.class)).getDamage();
            getWorld().removeObject(getOneIntersectingObject(SimpleBullet.class));
        }
        if(getOneIntersectingObject(Laser.class)!=null)
        {
            vida=vida-((Bala)getOneIntersectingObject(Laser.class)).getDamage();
            
        }
        if(getOneIntersectingObject(Misil.class)!=null)
        {
            vida=vida-((Bala)getOneIntersectingObject(Misil.class)).getDamage();
            getWorld().removeObject(getOneIntersectingObject(Misil.class));
        }
        if(getOneIntersectingObject(Rocket.class)!=null)
        {
            vida=vida-((Bala)getOneIntersectingObject(Rocket.class)).getDamage();
            getWorld().removeObject(getOneIntersectingObject(Rocket.class));
        }
        if(vida<0)
            vida=0;
    }
    
    /**
     * This method move the enemies.
     */    
    public void mueve()
    {
        int x=getX(), y=getY();
        
            if(moveE==move)
            {
                if(timer.millisElapsed()/100>10)
                {
                    newDir();
                    moveE=0;
                    timer.mark();
                }
            }
            else if((x>0&&x<getWorld().getWidth()-1)&&(y>0&&y<maxY))
            {
                switch(dir)
                {
                    case L:
                    if(x>20)
                        setLocation(x-10,y);                   
                    break;
                    case R:
                    if(x<getWorld().getWidth()-11)
                        setLocation(x+10,y);
                    break;
                    case U:
                    if(y>10)
                        setLocation(x,y-10);                            
                    break;
                    case D:
                    if(y<maxY-10)
                        setLocation(x,y+10);                            
                    break;
                }
                moveE+=10;
            }

    }
    
    /**
     * This method give one new random direction to the enemy.
     */
    public void newDir() 
    {
        int op=Greenfoot.getRandomNumber(4);
        switch(op)
        {
            case 0: dir= Direccion.L;
            break;
            case 1: dir= Direccion.R;
            break;
            case 2: dir= Direccion.U;
            break;
            case 3: dir= Direccion.D;
            break;
        }
    }
    
    /**
     * This method return the number of moves of the enemy.
     */
    public int getMoveE()
    {
        return moveE;
    }
    
    /**
     * This method return the life of the enemy.
     */
    public int getVida()
    {
        return vida;
    }
}
